//
//  YiJuApiMapping.m
//  RegexTest
//
//  Created by 冷悦 on 16/5/16.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "YiJuApiMapping.h"

@interface YiJuApiMapping ()

@property (nonatomic, strong) NSDictionary *mappingDict;

@end


@implementation YiJuApiMapping

#pragma mark - Initialize
- (instancetype)initWithUrlStr:(NSString *)urlStr{
    if (self = [super init]) {
        self.urlStr = urlStr;
    }
    
    return self;
}

#pragma mark - public
- (NSString *)doMapping{
    if (self.urlStr != nil) {
        NSArray *keys = [self.mappingDict allKeys];
        NSMutableString *replacedStrs = [NSMutableString new];
        
        for (NSString *key in keys) {
            [replacedStrs appendString:[NSString stringWithFormat:@"%@|", key]];
        }
        
        NSString *replacedStr = [replacedStrs substringToIndex:replacedStrs.length - 1];
        NSString *mappingStr = self.urlStr;
        NSString *returnStr = @"";
        
        while (YES) {
            returnStr = [self urlStr:mappingStr replacedStr:replacedStr];
            
            if ([returnStr isEqualToString:mappingStr]) {
                return returnStr;
            } else {
                mappingStr = returnStr;
            }
        }
    }
    
    return nil;
}

#pragma mark - Private
- (NSString *)urlStr:(NSString *)urlStr replacedStr:(NSString *)replacedStr {
    NSError *error;
    NSString *regexStr = [NSString stringWithFormat:@"%@%@%@", @"(.*/(", replacedStr, @")/)[0-9]+(.*)"];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr
                                                                           options:0
                                                                             error:&error];
    __block NSString *updatedStr = urlStr;
    [regex enumerateMatchesInString:urlStr
                            options:0
                              range:NSMakeRange(0, urlStr.length)
                         usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
                             NSString *match1 = [urlStr substringWithRange:[result rangeAtIndex:1]];
                             NSString *match2 = [urlStr substringWithRange:[result rangeAtIndex:3]];
                             NSString *key = [urlStr substringWithRange:[result rangeAtIndex:2]];
                             NSString *replaceStr = [self.mappingDict objectForKey:key];
                             updatedStr = [NSString stringWithFormat:@"%@%@%@", match1, replaceStr, match2];
                         }];
    return updatedStr;
}

#pragma mark - Getters
- (NSDictionary *)mappingDict {
    if (_mappingDict == nil) {
        _mappingDict = @{@"users":@"{userId}",
                         @"friends":@"{friendId}",
                         @"houses":@"{houseId}",
                         @"magazines":@"{magazineId}",
                         @"projects":@"{projectId}",
                         @"layouts":@"{layoutId}",
                         @"salers":@"{salerId}",
                         @"app":@"{appId}",
                         @"signs":@"{signId}",
                         @"softwareIcons":@"{iconVersion}",
                         @"feedbacks":@"{appId}",
                         @"appIcon":@"{appId}",
                         @"houses":@"{houseId}",
                         @"activities":@"{activityId}",
                         @"reservations":@"{reservationId}",
                         @"webType":@"{webType}",
                         @"webs":@"{webId}",
                         @"magazineType":@"{magazineType}"
                         };
    }
    
    return _mappingDict;
}




@end
